package org.dromara.module.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.module.domain.AdBanner;
import org.dromara.module.domain.CnPartner;
import org.dromara.module.service.ICnPartnerService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 合作伙伴列
 *
 * @author Lion Li
 * @date 2025-04-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/module/partner")
public class CnPartnerController extends BaseController {

    private final ICnPartnerService cnPartnerService;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;

    @Value("${ruoyi.profile}")
    private String uploadPath;

    /**
     * 查询合作伙伴列列表
     */
    @SaCheckPermission("module:partner:list")

    @GetMapping("/list")
    public TableDataInfo<CnPartner> list(CnPartner cnPartner, PageQuery pageQuery) {
        QueryWrapper<CnPartner> qw = new QueryWrapper<>();

        TableDataInfo<CnPartner> td = PageUtils.buildDataInfo(cnPartnerService.page(PageUtils.buildPage(),qw));
        List<CnPartner> list = td.getRows();
        for (CnPartner partner : list){
            if(StrUtil.isNotEmpty(partner.getLogo())){
                partner.setLogoPath(imgUrl + partner.getLogo());
            }
        }
        return td;
    }

    /**
     * 导出合作伙伴列列表
     */
    @SaCheckPermission("module:partner:export")
    @Log(title = "合作伙伴列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CnPartner cnPartner, HttpServletResponse response) {
        List<CnPartner> list = cnPartnerService.list();
        ExcelUtil.exportExcel(list, "合作伙伴列", CnPartner.class, response);
    }

    /**
     * 获取合作伙伴列详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("module:partner:query")
    @GetMapping("/{id}")
    public R<CnPartner> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        CnPartner cnPartner = cnPartnerService.getById(id);
        if(cnPartner != null){
            if(StrUtil.isNotEmpty(cnPartner.getLogo())){
                cnPartner.setLogoPath(imgUrl + cnPartner.getLogo());
            }
        }
        return R.ok(cnPartner);
    }

    /**
     * 新增合作伙伴列
     */
    @SaCheckPermission("module:partner:add")
    @Log(title = "合作伙伴列", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CnPartner cnPartner) {
        return toAjax(cnPartnerService.save(cnPartner));
    }

    /**
     * 修改合作伙伴列
     */
    @SaCheckPermission("module:partner:edit")
    @Log(title = "合作伙伴列", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CnPartner cnPartner) {
        return toAjax(cnPartnerService.updateById(cnPartner));
    }

    /**
     * 删除合作伙伴列
     *
     * @param ids 主键串
     */
    @SaCheckPermission("module:partner:remove")
    @Log(title = "合作伙伴列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(cnPartnerService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
